using System;
using UnityEngine;
using Object = UnityEngine.Object;

namespace QFramework.RollABall
{
    public class Player
    {
        private GameObject mPlayerObj { get; set; }
        
        private Rigidbody mRigidBody { get; set; }

        /// <summary>
        /// GameCamera 需要获取到 Player 的 GameObject 
        /// </summary>
        public GameObject GameObject
        {
            get { return mPlayerObj; }
        }

        public Player()
        {
            // 创建 玩家
            mPlayerObj = Fluent.Sphere("Player")
                .Position(new Vector3(0, 0.5f, 0))
                .Build();

            mRigidBody = mPlayerObj.AddComponent<Rigidbody>();
        }

        /// <summary>
        /// 开始监听用户输入
        /// </summary>
        public void StartListenUserInput()
        {
            Fluent.MonoBehaviour(mPlayerObj)
                .OnFixedUpdate(() =>
                {
                    var moveHorizontal = Input.GetAxis("Horizontal");
                    var moveVertical = Input.GetAxis("Vertical");

                    var movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

                    mRigidBody.AddForce(movement * 5);
                })
                .Build();
        }

        /// <summary>
        /// 开始检测碰撞
        /// </summary>
        public void StartCheckCollision(Action onPickUp)
        {
            Fluent.OnTriggerEnterBuilder(mPlayerObj)
                .OnTriggerEnter(other =>
                {
                    if (other.gameObject.name == "Pick Up")
                    {
                        Object.Destroy(other.gameObject);
                        
                        onPickUp();
                    }
                })
                .Build();
        }
    }
}